﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Document_Management
{
    public partial class VehicleSearch : Form
    {
        TextBox tx;

        static string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["庫存管理"].ConnectionString;
        SqlConnection con = new SqlConnection(connectionString);
        public VehicleSearch(TextBox tx)
        {
            InitializeComponent();
            this.tx = tx;
        }

        private void VehicleSearch_Load(object sender, EventArgs e)
        {
            dataGridSearch.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            if (tx.Name.ToString().Equals("txt_車輛_車種代號"))
            {
                Load_車種();
            }
            if (tx.Name.ToString().Equals("txt_車輛_人員代號"))
            {
                Load_車輛人員資料();
            }
            if (tx.Name.ToString().Equals("txt_車輛_型號"))
            {
                Load_車輛型號訊息();
            }
            if (tx.Name.ToString().Equals("txt_車輛_廠牌代號"))
            {
                Load_車輛廠牌();
            }
            if (tx.Name.ToString().Equals("txt_車輛_維修日期代號"))
            {
                Load_車輛維修日期();
            }
            if (tx.Name.ToString().Equals("txt_成本中心"))
            {
                Load_車輛成本中心();
            }
            if (tx.Name.ToString().Equals("txt_存放地點"))
            {
                Load_手工具存放地點();
            }
            if (tx.Name.ToString().Equals("txt_廠牌"))
            {
                Load_手工具廠牌();
            }
            if (tx.Name.ToString().Equals("txt_工具類別"))
            {
                Load_手工具類別();
            }
            if (tx.Name.ToString().Equals("txt_請購人員"))
            {
                Load_請購人員();
            }
            if (tx.Name.ToString().Equals("txt_領用人"))
            {
                Load_手工具領用人("sp_手工具領用人_load");
            }
            if (tx.Name.ToString().Equals("txt_借用人"))
            {
                Load_手工具領用人("sp_手工具借用人_load");
            }
            if (tx.Name.ToString().Equals("txt_受理人"))
            {
                Load_手工具受理人();
            }
            //txt_手工具領用部門代號
            if (tx.Name.ToString().Equals("txt_手工具領用部門代號"))
            {
                Load_手工具領用人("sp_手工具領用部門_load");
            }
        }

        private void bt_查詢_Click(object sender, EventArgs e)
        {
            if (tx.Name.ToString().Equals("txt_車輛_車種代號"))
            {
                search("sp_車種_search");
            }
            if (tx.Name.ToString().Equals("txt_車輛_人員代號"))
            {
                search("sp_車輛人員資料_search");
            }
            if (tx.Name.ToString().Equals("txt_車輛_型號"))
            {
                search("sp_車輛型號訊息_search");
            }
            if (tx.Name.ToString().Equals("txt_車輛_廠牌代號"))
            {
                search("sp_車輛廠牌_search");
            }
            if (tx.Name.ToString().Equals("txt_成本中心"))
            {
                search("sp_車輛成本中心_search");
            }
            
            if (tx.Name.ToString().Equals("txt_車輛_維修日期代號"))
            {
                try
                {
                    con.Open();
                    string sp_search = "sp_車輛維修日期_search";
                    SqlDataAdapter cmd = new SqlDataAdapter(sp_search, con);
                    cmd.SelectCommand.CommandType = CommandType.StoredProcedure;
                    if (txt_查詢.Text != "") cmd.SelectCommand.Parameters.AddWithValue("@姓名或代號", Int32.Parse(txt_查詢.Text.ToString()));
                    DataTable dt = new DataTable();
                    cmd.Fill(dt);
                    dataGridSearch.DataSource = dt;
                    con.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            if (tx.Name.ToString().Equals("txt_存放地點"))
            {
                search("sp_手工具_手工具存放地點_search");
            }
            if (tx.Name.ToString().Equals("txt_廠牌"))
            {
                search("sp_手工具_手工具廠牌_search");
            }
            if (tx.Name.ToString().Equals("txt_工具類別"))
            {
                search("sp_手工具_手工具類別_search");
            }
            if (tx.Name.ToString().Equals("txt_請購人員"))
            {
                search("sp_手工具_請購人員_search");
            }
        }

        public void search(String sp_search)
        {
            try
            {
                con.Open();
                SqlDataAdapter cmd = new SqlDataAdapter(sp_search, con);
                cmd.SelectCommand.CommandType = CommandType.StoredProcedure;
                if (txt_查詢.Text != "") cmd.SelectCommand.Parameters.AddWithValue("@姓名或代號", txt_查詢.Text);
                DataTable dt = new DataTable();
                cmd.Fill(dt);
                dataGridSearch.DataSource = dt;
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        public void Load_車輛成本中心()
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter("sp_load_車輛成本中心", con);
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                DataTable dt = new DataTable();
                da.Fill(dt);
                dataGridSearch.DataSource = dt;
                PublicClass.setRowNumber(dataGridSearch);
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                con.Close();
            }
        }

        public void Load_車輛人員資料()
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {   
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter("sp_車輛人員資料_load", con);
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                DataTable dt = new DataTable();
                da.Fill(dt);
                dataGridSearch.DataSource = dt;
                PublicClass.setRowNumber(dataGridSearch);
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                con.Close();
            }
        }

        public void Load_車輛廠牌()
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter("sp_車輛廠牌_load", con);
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                DataTable dt = new DataTable();
                da.Fill(dt);
                dataGridSearch.DataSource = dt;
                PublicClass.setRowNumber(dataGridSearch);
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                con.Close();
            }
        }

        public void Load_手工具領用人(string procedure)
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter(procedure, con);
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                DataTable dt = new DataTable();
                da.Fill(dt);
                dataGridSearch.DataSource = dt;
                PublicClass.setRowNumber(dataGridSearch);
                dataGridSearch.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                con.Close();
            }
        }

        public void Load_手工具受理人()
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter("sp_手工具受理人_load", con);
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                DataTable dt = new DataTable();
                da.Fill(dt);
                dataGridSearch.DataSource = dt;
                PublicClass.setRowNumber(dataGridSearch);
                dataGridSearch.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                con.Close();
            }
        }

        public void Load_手工具廠牌()
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter("sp_手工具廠牌_load", con);
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                DataTable dt = new DataTable();
                da.Fill(dt);
                dataGridSearch.DataSource = dt;
                PublicClass.setRowNumber(dataGridSearch);
                dataGridSearch.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                con.Close();
            }
        }

        public void Load_手工具類別()
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter("sp_手工具類別_load", con);
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                DataTable dt = new DataTable();
                da.Fill(dt);
                dataGridSearch.DataSource = dt;
                PublicClass.setRowNumber(dataGridSearch);
                dataGridSearch.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                con.Close();
            }
        }

        public void Load_請購人員()
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter("sp_請購人員_load", con);
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                DataTable dt = new DataTable();
                da.Fill(dt);
                dataGridSearch.DataSource = dt;
                PublicClass.setRowNumber(dataGridSearch);
                dataGridSearch.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                con.Close();
            }
        }

        public void Load_車種()
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter("sp_車種_load", con);
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                DataTable dt = new DataTable();
                da.Fill(dt);
                dataGridSearch.DataSource = dt;
                PublicClass.setRowNumber(dataGridSearch);
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                con.Close();
            }
        }

        public void Load_車輛型號訊息()
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter("sp_車輛型號訊息_load", con);
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                DataTable dt = new DataTable();
                da.Fill(dt);
                dataGridSearch.DataSource = dt;
                PublicClass.setRowNumber(dataGridSearch);
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                con.Close();
            }
        }

        public void Load_車輛維修日期()
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter("sp_車輛維修日期_load", con);
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                DataTable dt = new DataTable();
                da.Fill(dt);
                dataGridSearch.DataSource = dt;
                PublicClass.setRowNumber(dataGridSearch);
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                con.Close();
            }
        }

        public void Load_手工具存放地點()
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter("sp_手工具存放地點_load", con);
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                DataTable dt = new DataTable();
                da.Fill(dt);
                dataGridSearch.DataSource = dt;
                PublicClass.setRowNumber(dataGridSearch);
                dataGridSearch.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                con.Close();
            }
        }

        private void bt_確定_Click(object sender, EventArgs e)
        {
            try
            {
                int r = dataGridSearch.CurrentCell.RowIndex;
                string text = dataGridSearch.Rows[r].Cells[0].Value.ToString();
                tx.Text = text;
                this.Close();
            }
            catch
            {
                MessageBox.Show("資料不對");
            }
        }

    }
}
